package com.handy.plugin.data;

import javax.script.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 脚本解释器
 * @author rocken.zeng@gmail.com
 *
 */
public class ScriptCompile {
	
	public static void scriptProperty(ScriptBean bean) {
		if (bean.getFileContent()==null || "".equals(bean.getFileContent())) return;
		
		ScriptEngineManager factory = new ScriptEngineManager();
		ScriptEngine engine = factory.getEngineByName("JavaScript");
		Compilable compilable = (Compilable) engine;
		try {
			CompiledScript compiled = compilable.compile(bean.getFileContent());
			engine.put("run", false);//此时一定要设置为false，只取脚本前定义的属性参数。
			compiled.eval();
			// v_script_id
			String scriptId = (String) engine.getContext().getAttribute("v_script_id");
			// v_status
			if (engine.getContext().getAttribute("v_status") != null && !"".equals(engine.getContext().getAttribute("v_status"))){
				Integer status = Integer.valueOf((String)engine.getContext().getAttribute("v_status"));
				bean.setStatus(status);
			}
			bean.setId(scriptId);
		} catch (ScriptException e) {
			log.error(e.getMessage());
		}
	}
	
	public static void invoke(){
		
	}
	
//	public static void invokeFindTrue(ScriptBean bean,HashMap<String,String> vars,HttpSession session,DBUtil dbUtil) {
//		ScriptEngineManager factory = new ScriptEngineManager();
//		ScriptEngine engine = factory.getEngineByName("JavaScript");
//		Compilable compilable = (Compilable) engine;
//		try {
//			CompiledScript compiled = compilable.compile(bean.getFileString());
//			engine.put("run", true);//此时一定要设置为false，只取脚本前定义的特殊参数。
//			engine.put("vars", vars);
//			engine.put("session", session);
//			engine.put("dbUtil", dbUtil);
//			compiled.eval();
//			// v_p_table_name
//			bean.getParams().put("v_table_name", engine.getContext().getAttribute("v_table_name"));
//			// v_columns
//			bean.getParams().put("v_columns", engine.getContext().getAttribute("v_columns"));
//			// v_condition
//			bean.getParams().put("v_condition", engine.getContext().getAttribute("v_condition"));
//			// v_max_line
//			bean.getParams().put("v_max_line", engine.getContext().getAttribute("v_max_line"));
//			// v_page_list_count
//			bean.getParams().put("v_page_list_count", engine.getContext().getAttribute("v_page_list_count"));
//		} catch (ScriptException e) {
//			log.error(e.getMessage());
//		}
//	}
//	
//	public static String invokeTrue(ScriptBean bean,HashMap<String,String> vars,HttpSession session,DBUtil dbUtil){
//		String error = "";
//		ScriptEngineManager factory = new ScriptEngineManager();
//		ScriptEngine engine = factory.getEngineByName("JavaScript");
//		Compilable compilable = (Compilable) engine;
//		try {
//			CompiledScript compiled = compilable.compile(bean.getFileString());
//			engine.put("run", true);//此时一定要设置为false，只取脚本前定义的特殊参数。
//			engine.put("vars", vars);
//			engine.put("session", session);
//			engine.put("dbUtil", dbUtil);
//			compiled.eval();
//		} catch (ScriptException e) {
//			error = e.getMessage();
//			log.error(e.getMessage());
//		}
//		return error;
//	}
//
	private static Log log = LogFactory.getLog(ScriptCompile.class);
	
}
